import { defineStore } from 'pinia'
import { ref } from 'vue'
import { reqLogin, reqUserInfo } from '@/api/user'
import { constantRoutes } from '@/router/routes.js'

const useUserStore = defineStore('User', () => {
  let token = ref(localStorage.getItem('TOKEN'))
  let username = ref('')
  let avatar = ref('')
  // 路由数据
  const menuRoutes = constantRoutes
  // 用户数据
  const userInfo = ref([
    {
      userId: 1,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'admin',
      password: '123456',
      roles: ['超级管理员'],
      token: 'Admin Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 2,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user1',
      password: '123456',
      roles: ['普通管理员'],
      token: 'User1 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
    {
      userId: 3,
      avatar:
        'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      username: 'user2',
      password: '123456',
      roles: ['测试'],
      token: 'User2 Token',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
    },
  ])

  // 模拟角色数据 - 添加 permissions 字段
  const roleList = ref([
    {
      roleId: 1,
      roleName: '超级管理员',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
      permissions: ['/', '/home', '/screen', '/acl', '/acl/user', '/acl/role', '/acl/menu']
    },
    {
      roleId: 2,
      roleName: '普通管理员',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
      permissions: ['/', '/home', '/acl', '/acl/user']
    },
    {
      roleId: 3,
      roleName: '测试',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
      permissions: ['/', '/home']
    },
    {
      roleId: 4,
      roleName: '普通用户',
      createTime: '2025-01-15 10:30:00',
      updateTime: '2025-06-20 14:45:00',
      permissions: ['/', '/home']
    }
  ])

  const userLogin = async (data) => {
    const res = await reqLogin(data)
    if (res.code === 200) {
      token.value = res.data.token
      localStorage.setItem('TOKEN', res.data.token)
      return '登录成功'
    } else {
      return Promise.reject(new Error(res.data.message))
    }
  }
  const getUserInfo = async () => {
    const res = await reqUserInfo()
    if (res.code === 200) {
      username.value = res.data.checkUser.username
      avatar.value = res.data.checkUser.avatar
      return '获取用户信息成功'
    } else {
      return Promise.reject(new Error(res.data.message))
    }
  }
  const clearUserInfo = () => {
    token.value = ''
    username.value = ''
    avatar.value = ''
    localStorage.removeItem('TOKEN')
  }

  // 添加用户方法
  const addUser = (newUser) => {
    userInfo.value.push(newUser)
  }

  // 删除用户方法
  const deleteUser = (userId) => {
    const index = userInfo.value.findIndex(user => user.userId === userId)
    if (index !== -1) {
      userInfo.value.splice(index, 1)
      return true
    }
    return false
  }

  // 批量删除用户方法
  const deleteUsers = (userIds) => {
    userInfo.value = userInfo.value.filter(user => !userIds.includes(user.userId))
  }

  // 添加角色方法
  const addRole = (newRole) => {
    roleList.value.push(newRole)
  }

  // 删除角色方法
  const deleteRole = (roleId) => {
    const index = roleList.value.findIndex(role => role.roleId === roleId)
    if (index !== -1) {
      roleList.value.splice(index, 1)
      return true
    }
    return false
  }

  // 更新角色权限方法
  const updateRolePermissions = (roleId, permissions) => {
    const role = roleList.value.find(role => role.roleId === roleId)
    if (role) {
      // 创建新数组，避免引用共享
      role.permissions = [...permissions]
      role.updateTime = new Date().toLocaleString('zh-CN')
      return true
    }
    return false
  }

  // 获取角色权限方法
  const getRolePermissions = (roleId) => {
    const role = roleList.value.find(role => role.roleId === roleId)
    // 确保返回新数组，避免引用共享
    return role ? [...role.permissions] : []
  }

  return {
    token,
    username,
    avatar,
    menuRoutes,
    userInfo,
    roleList,
    userLogin,
    getUserInfo,
    clearUserInfo,
    addUser,
    deleteUser,
    deleteUsers,
    addRole,
    deleteRole,
    updateRolePermissions,
    getRolePermissions
  }
})

export default useUserStore